﻿using ImageManager.DBAccess;
using ImageManager.Model;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;

namespace ImageManager.ImageApi
{
    public static class QueryConditionResolver
    {
        public static CondCollection Resolver<T>(dynamic qryParam) where T : BaseModel, new()
        {
            CondCollection ret = CondCollection.True();
            if (qryParam == null)
            {
                return ret;
            }
            var datAsDic = qryParam as JObject;
            if (datAsDic == null)
            {
                return ret;
            }
            var pils = typeof(T).GetProperties();
            foreach (var pi in pils)
            {
                if (datAsDic.ContainsKey("S_" + pi.Name))
                {
                    WhereConditional cond = new WhereConditional(pi.Name);
                    MatchValue(datAsDic["S_" + pi.Name].ToString(), cond);
                    ret.AddWhereCond(cond);
                }
                for (int i = 0; i < 8; i++)
                {
                    //同一搜索关键字支持最多8个不同的值作为条件，用于查找范围区间(ge,le)等场景
                    string key = "S_" + pi.Name + "_" + i;
                    if (datAsDic.ContainsKey(key))
                    {
                        WhereConditional cond = new WhereConditional(pi.Name);
                        MatchValue(datAsDic[key].ToString(), cond);
                        ret.AddWhereCond(cond);
                    }
                }
            }
            return ret;
        }

        private static void MatchValue(string val, WhereConditional conditional)
        {
            if (val == null) return;
            string reg1 = "\\([\\s\\S]*\\)$";
            if (Regex.IsMatch(val, "^eq" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Eq;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^gt" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Gt;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^lt" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Lt;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^ge" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Ge;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^le" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.Le;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "^lk" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.FullLk;
                conditional.Value = "%" + val.Substring(3, val.Length - 4) + "%";
            }
            if (Regex.IsMatch(val, "^nk" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.NotLk;
                conditional.Value = "%" + val.Substring(3, val.Length - 4) + "%";
            }
            if (Regex.IsMatch(val, "^in" + reg1, RegexOptions.IgnoreCase))
            {
                var value = val.Substring(3, val.Length - 4);
                if (value.IsNullOrEmpty())
                {
                    conditional.Op = OpEnum.IsNull;
                    return;
                }
                conditional.Op = OpEnum.In;
                conditional.Value = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }
            if (Regex.IsMatch(val, "ne" + reg1, RegexOptions.IgnoreCase))
            {
                conditional.Op = OpEnum.NotEq;
                conditional.Value = val.Substring(3, val.Length - 4);
            }
            if (Regex.IsMatch(val, "nv" + reg1, RegexOptions.IgnoreCase))
            {
                //nv(Y) 是空，nv(N)是非空
                var flag = val.Substring(3, val.Length - 4);
                if (flag.ToUpper() == "Y")
                {
                    conditional.Op = OpEnum.IsNull;
                }
                if (flag.ToUpper() == "N")
                {
                    conditional.Op = OpEnum.IsNotNull;
                }
            }
        }
    }
}
